/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.dao.query.builder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.openinsula.vulcano.orm.dao.query.DaoQuery;
import org.openinsula.vulcano.orm.dao.query.DistinctDaoQuery;
import org.openinsula.vulcano.orm.dao.query.ProjectionDaoQuery;
import org.openinsula.vulcano.orm.dao.query.SimpleDaoQuery;
import org.openinsula.vulcano.orm.dao.query.projection.CountProjection;
import org.openinsula.vulcano.orm.dao.query.projection.MaxProjection;
import org.openinsula.vulcano.orm.dao.query.projection.MinProjection;
import org.openinsula.vulcano.orm.dao.query.projection.Projection;
import org.openinsula.vulcano.orm.dao.query.projection.SumProjection;
import org.openinsula.vulcano.orm.dao.query.spec.AndSpec;
import org.openinsula.vulcano.orm.dao.query.spec.BetweenSpec;
import org.openinsula.vulcano.orm.dao.query.spec.EqualsSpec;
import org.openinsula.vulcano.orm.dao.query.spec.GreaterOrEqualSpec;
import org.openinsula.vulcano.orm.dao.query.spec.GreaterSpec;
import org.openinsula.vulcano.orm.dao.query.spec.InSpec;
import org.openinsula.vulcano.orm.dao.query.spec.LesserOrEqualSpec;
import org.openinsula.vulcano.orm.dao.query.spec.LesserSpec;
import org.openinsula.vulcano.orm.dao.query.spec.LikeSpec;
import org.openinsula.vulcano.orm.dao.query.spec.NotEqualsSpec;
import org.openinsula.vulcano.orm.dao.query.spec.NotNullSpec;
import org.openinsula.vulcano.orm.dao.query.spec.NullSpec;
import org.openinsula.vulcano.orm.dao.query.spec.Spec;

/**
 * Builder implementation to ease {@link DaoQuery} creation.
 * 
 * @author yanaga
 * @since 1.1
 * @see DaoQuery
 * @param T The return type of the returned {@link DaoQuery}
 */
public class DaoSelect<T> {

	private boolean distinct = false;
	
	private DaoQuery<T> daoQuery = null;
	
	private Projection projection = null;
	
	private List<Spec> specList = new ArrayList<Spec>();
	
	public DaoSelect() {
		this(false);
	}
	
	public DaoSelect(boolean distinct) {
		this.distinct = distinct;
	}
	
	public BindedFrom all() {
		this.projection = null;
		return new BindedFrom();
	}
	
	public From count() {
		this.projection = new CountProjection();
		return new From();
	}

	public From max(String property) {
		this.projection = new MaxProjection(property);
		return new From();
	}

	public From min(String property) { 
		this.projection = new MinProjection(property);
		return new From();
	}

	public From sum(String property) {
		this.projection = new SumProjection(property);
		return new From();
	}
	
	protected DaoQuery<T> toQuery() {
		if (specList.size() > 0) {
			daoQuery.setSpec(new AndSpec(specList));
		}
		return daoQuery;
	}

	/**
	 * 
	 * @author yanaga
	 *
	 */
	public class BindedFrom {
		public Where from(Class<T> fromClass) {
			if (distinct) {
				daoQuery = new DistinctDaoQuery<T>(fromClass);
			} else {
				daoQuery = new SimpleDaoQuery<T>(fromClass);
			}
			return new Where();
		}
	}
	
	/**
	 * 
	 * @author yanaga
	 *
	 */
	public class From {
		public Where from(Class<?> fromClass) {
			daoQuery = new ProjectionDaoQuery<T>(fromClass, projection);
			return new Where();
		}
	}
	
	/**
	 * 
	 * @author yanaga
	 *
	 */
	public class Where {

		public Where spec(Spec spec) {
			specList.add(spec);
			return this;
		}
		
		public Where eq(String propertyName, Object value) {
			specList.add(new EqualsSpec(propertyName, value));
			return this;
		}

		public Where ne(String propertyName, Object value) {
			specList.add(new NotEqualsSpec(propertyName, value));
			return this;
		}

		public Where gt(String propertyName, Object value) {
			specList.add(new GreaterSpec(propertyName, value));
			return this;
		}

		public Where ge(String propertyName, Object value) {
			specList.add(new GreaterOrEqualSpec(propertyName, value));
			return this;
		}

		public Where lt(String propertyName, Object value) {
			specList.add(new LesserSpec(propertyName, value));
			return this;
		}

		public Where le(String propertyName, Object value) {
			specList.add(new LesserOrEqualSpec(propertyName, value));
			return this;
		}

		public Where between(String propertyName, Object firstValue, Object secondValue) {
			specList.add(new BetweenSpec(propertyName, firstValue, secondValue));
			return this;
		}

		public Where in(String propertyName, Object... values) {
			specList.add(new InSpec(propertyName, values));
			return this;
		}

		public Where like(String propertyName, String value) {
			specList.add(new LikeSpec(propertyName, value));
			return this;
		}

		public Where ilike(String propertyName, String value) {
			specList.add(new LikeSpec(propertyName, value, false));
			return this;
		}

		public Where isNull(String propertyName) {
			specList.add(new NullSpec(propertyName));
			return this;
		}

		public Where isNotNull(String propertyName) {
			specList.add(new NotNullSpec(propertyName));
			return this;
		}
		
		public Order order(String... propertyNames) {
			return new Order(propertyNames);
		}
		
		public First first(int firstIndex) {
			return new First(firstIndex);
		}

		public Limit limit(int limitSize) {
			return new Limit(limitSize);
		}

		public DaoQuery<T> toQuery() {
			return DaoSelect.this.toQuery();
		}
	}
	
	/**
	 * 
	 * @author yanaga
	 *
	 */
	public class Order {

		public Order(String... orders) {
			daoQuery.getOrderParameters().addAll(Arrays.asList(orders));
		}
		
		public First first(int firstIndex) {
			return new First(firstIndex);
		}
		
		public Limit limit(int limitSize) {
			return new Limit(limitSize);
		}
		
		public Lock lock() {
			return new Lock();
		}
		
		public DaoQuery<T> toQuery() {
			return DaoSelect.this.toQuery();
		}
		
	}

	/**
	 * 
	 * @author yanaga
	 *
	 */
	public class First {

		public First(int firstIndex) {
			daoQuery.setFirstResult(firstIndex);
		}
		
		public Limit limit(int limitSize) {
			return new Limit(limitSize);
		}
		
		public DaoQuery<T> toQuery() {
			return DaoSelect.this.toQuery();
		}
		
	}
	
	/**
	 * 
	 * @author yanaga
	 *
	 */
	public class Limit {

		public Limit(int limitSize) {
			daoQuery.setLimit(limitSize);
		}
		
		public Lock lock() {
			return new Lock();
		}
		
		public DaoQuery<T> toQuery() {
			return DaoSelect.this.toQuery();
		}

	}

	/**
	 * 
	 * @author yanaga
	 *
	 */
	public class Lock {

		public Lock() {
			daoQuery.setLock(true);
		}
		
		public DaoQuery<T> toQuery() {
			return DaoSelect.this.toQuery();
		}
		
	}

}